package com.stylefeng.guns.modular.station.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.area.dao.BdapAreaCellRefMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapCellInfoMonthMapper;
import com.stylefeng.guns.modular.area.dao.BdapCityDistrictMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubAreaMapper;
import com.stylefeng.guns.modular.area.model.BdapArea;
import com.stylefeng.guns.modular.area.model.BdapAreaCellRef;
import com.stylefeng.guns.modular.area.model.BdapCellInfoMonth;
import com.stylefeng.guns.modular.area.model.BdapCityDistrict;
import com.stylefeng.guns.modular.area.model.BdapSubArea;
import com.stylefeng.guns.modular.area.model.Station;
import com.stylefeng.guns.modular.util.EntityUtil;
import com.stylefeng.guns.modular.util.FileUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 基站管理控制器
 *
 * @author fengshuonan
 * @Date 2017-12-14 15:52:57
 */
@Controller
@RequestMapping("/station")
public class StationController extends BaseController {

    private String PREFIX = "/station/";
    
    @Autowired
	private BdapCellInfoMonthMapper cellMapper;
    
    @Autowired
	private BdapCityDistrictMapper cityDistrictMapper;
    
    @Autowired
	private BdapAreaMapper areaMapper;
    
    @Autowired
   	private BdapAreaCellRefMapper areaCellMapper;
    
    @Autowired
   	private BdapSubAreaMapper subAreaMapper;
    /**
     * 跳转到基站管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "station.html";
    }

    /**
     * 跳转到添加基站管理
     */
    @RequestMapping("/station_add")
    public String stationAdd(Model model) {
    	List<BdapCityDistrict> cityDistrictList = cityDistrictMapper.selectList(null);
		Map<String, List<String>> cityDistrictMap = EntityUtil.getCityDistrictMap(cityDistrictList);
		String cityDistrictJson = JSON.toJSONString(cityDistrictMap).replaceAll("\"", "\'");
		model.addAttribute("cityDistrictJson", cityDistrictJson);
        return PREFIX + "station_add.html";
    }

    /**
     * 跳转到修改基站管理
     */
    @RequestMapping("/station_update/{lac}/{ci}")
    public String stationUpdate(@PathVariable String lac,@PathVariable String ci, Model model) {
    	EntityWrapper<BdapCellInfoMonth> cellEntityWrapper = new EntityWrapper<>();
    	cellEntityWrapper.eq("lac", lac).and().eq("ci", ci);
    	BdapCellInfoMonth cell = cellMapper.selectList(cellEntityWrapper).get(0);
    	model.addAttribute("cell", cell);
    	List<BdapCityDistrict> cityDistrictList = cityDistrictMapper.selectList(null);
		Map<String, List<String>> cityDistrictMap = EntityUtil.getCityDistrictMap(cityDistrictList);
		String cityDistrictJson = JSON.toJSONString(cityDistrictMap).replaceAll("\"", "\'");
		model.addAttribute("cityDistrictJson", cityDistrictJson);
		return PREFIX + "station_edit.html";
    }

    /**
     * 获取基站管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition,@RequestParam(required = false) String city,@RequestParam(required = false) String county,String isAreaCell) {
    	List<Station> stationList = cellMapper.selectStation(condition,city,county);
		return stationList;
    }

    /**
     * 新增基站管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(BdapCellInfoMonth cell) {
    	Calendar cale = Calendar.getInstance();
    	int year = cale.get(Calendar.YEAR);
    	int month = cale.get(Calendar.MONTH) + 1;
    	String time = month>9?String.valueOf(year)+String.valueOf(month):String.valueOf(year)+"0"+String.valueOf(month);
    	cell.setTime(time);
    	return cellMapper.insert(cell);
    }

    /**
     * 删除基站管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(String lac,String ci) {
    	EntityWrapper<BdapCellInfoMonth> cellEntityWrapper = new EntityWrapper<>();
    	cellEntityWrapper.eq("lac", lac).and().eq("ci", ci);
    	return cellMapper.delete(cellEntityWrapper);
    }

    /**
     * 修改基站管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BdapCellInfoMonth cell) {
    	EntityWrapper<BdapCellInfoMonth> cellEntityWrapper = new EntityWrapper<>();
    	cellEntityWrapper.eq("lac", cell.getLac()).and().eq("ci", cell.getCi());
    	Calendar cale = Calendar.getInstance();
    	int year = cale.get(Calendar.YEAR);
    	int month = cale.get(Calendar.MONTH) + 1;
    	String time = month>9?String.valueOf(year)+String.valueOf(month):String.valueOf(year)+"0"+String.valueOf(month);
    	cell.setTime(time);
        return cellMapper.update(cell, cellEntityWrapper);
    }
    
    /**
     * 下载区域基站
     */
    @RequestMapping(value = "/areaStationDownload")
    @ResponseBody
    public Object download(String fileName,HttpServletResponse response) {
//    	EntityWrapper<BdapAreaCellRef> areaCellWrapper = new EntityWrapper<>();
//    	areaCellWrapper.setSqlSelect("lac_Hex_Code,ci_Hex_Code").groupBy("lac_Hex_Code,ci_Hex_Code");
    	List<BdapAreaCellRef> areaCellList = areaCellMapper.selectList(null);
    	List<String> dataList = new ArrayList<String>();
    	for(BdapAreaCellRef areaCell:areaCellList) {
    		dataList.add(areaCell.toString());
    	}
    	FileUtil.download(fileName, dataList, response);
    	return SUCCESS_TIP;
    }
}
